package com.sdp.irrigation.db.irrigation_emp_pump_well.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.core.bizc.db.IViewQuery;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.irrigation.db.irrigation_emp_field.service.IIrrigationEmpFieldService;
import com.sdp.irrigation.db.irrigation_emp_pump_well.mapper.IrrigationEmpPumpWellMapper;
import com.sdp.irrigation.db.irrigation_emp_pump_well.service.IIrrigationEmpPumpWellService;
import com.sdp.irrigation.db.irrigation_field.service.IIrrigationFieldService;
import com.sdp.irrigation.db.irrigation_pump_well.service.impl.IrrigationPumpWellServiceImpl;
import com.sdp.irrigation.entity.IrrigationEmpField;
import com.sdp.irrigation.entity.IrrigationEmpPumpWell;
import com.sdp.irrigation.entity.IrrigationField;
import com.sdp.irrigation.utils.IrrigationConstants;
import com.sdp.irrigation.view.IrrigationPumpWellView;

/**
 * <p>
 * 人员机井权限信息 服务实现类
 * </p>
 *
 * @author zsp
 * @since 2024-12-22
 */
@Service
public class IrrigationEmpPumpWellServiceImpl extends ServiceImpl<IrrigationEmpPumpWellMapper, IrrigationEmpPumpWell> implements IIrrigationEmpPumpWellService,IViewQuery{
	
	@Autowired
	private IIrrigationFieldService iIrrigationFieldService;
	
	@Autowired
	private IrrigationPumpWellServiceImpl irrigationPumpWellServiceImpl;
	
	@Autowired
	private IIrrigationEmpFieldService iIrrigationEmpFieldService;
	
	@Autowired
	private IrrigationEmpPumpWellMapper irrigationEmpPumpWellMapper;
	
	@Override
	public List<JSONObject> queryPumpWellTree(String projectNo) {
		List<JSONObject> treeList = new ArrayList<JSONObject>();
		
		//根据项目获取田块
		List<IrrigationField> fieldList = iIrrigationFieldService.queryFieldByProjectNo(projectNo);
		if(fieldList==null || fieldList.size()==0) {
			return treeList;
		}
		//获取项目下的机井
		QueryWrapper<IrrigationPumpWellView> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("a.project_no", projectNo);
		List<IrrigationPumpWellView> pumpWellList = irrigationPumpWellServiceImpl.viewList(queryWrapper,null);
		if(pumpWellList==null || pumpWellList.size()==0) {
			return treeList;
		}
		
		JSONObject treeNode = null;
		for(IrrigationField field : fieldList) {
			treeNode = new JSONObject();
			treeNode.put("key", IrrigationConstants.机井树节点类型_田块.getCode()+"#"+field.getFieldNo());
			treeNode.put("value", field.getFieldNo());
			treeNode.put("title", field.getFieldName());
			treeNode.put("type", IrrigationConstants.机井树节点类型_田块.getCode());
			treeList.add(treeNode);
			getChildTreeNode(treeNode, pumpWellList);
		}
		
		return treeList;
	}

	@SuppressWarnings({ "unchecked" })
	private void getChildTreeNode(JSONObject parentObj,List<IrrigationPumpWellView> pumpWellList) {
		List<JSONObject> childList = new ArrayList<JSONObject>();
		for(int j=0;j<pumpWellList.size();j++) {
			if(!StringUtils.isEmpty(pumpWellList.get(j).getFieldNo()) && parentObj.getString("value").equals(pumpWellList.get(j).getFieldNo())) {
				if(parentObj.get("children")==null) {
					parentObj.put("children", childList);
				}else {
					childList = (List<JSONObject>) parentObj.get("children");
				}
				
				JSONObject childObj = new JSONObject();
				childObj.put("key", IrrigationConstants.机井树节点类型_机井.getCode()+"#"+pumpWellList.get(j).getPwNo());
				childObj.put("value", pumpWellList.get(j).getPwNo());
				childObj.put("title", pumpWellList.get(j).getPwName());
				childObj.put("type", IrrigationConstants.机井树节点类型_机井.getCode());
				childList.add(childObj);
			}
		}
	}

	@Override
	public void setEmpPumpWell(String empNo,JSONObject checked) {
		JSONArray halfCheckedKeys = checked.getJSONArray("halfCheckedKeys");
		JSONArray checkedKeys = checked.getJSONArray("checkedKeys");
		String projectNo = checked.getString("projectNo");
		//首先删除原来的人员机井信息
		removeEmpPumpWellByEmpNo(projectNo,empNo);
		
		//删除原来的人员田块信息
		iIrrigationEmpFieldService.removeEmpFieldByEmpNo(projectNo,empNo);
		
		IrrigationEmpField empField = null;
		IrrigationEmpPumpWell empPumpWell = null;
		List<IrrigationEmpField> empFieldList = new ArrayList<>();
		List<IrrigationEmpPumpWell> empPumpWellList = new ArrayList<>();
		
		if(halfCheckedKeys!=null && halfCheckedKeys.size()>0) {
			for (int i = 0; i < halfCheckedKeys.size(); i++) {
				if(halfCheckedKeys.getString(i).indexOf(IrrigationConstants.机井树节点类型_机井.getCode())>-1) {
					empPumpWell = new IrrigationEmpPumpWell();
					addIrrigationEmpPumpWell(empPumpWellList,projectNo,empNo, halfCheckedKeys.getString(i), empPumpWell);
				}else {
					empField = new IrrigationEmpField();
					addIrrigationEmpField(empFieldList,projectNo,empNo, halfCheckedKeys.getString(i), empField);
				}
			}
		}
		
		if(checkedKeys!=null && checkedKeys.size()>0) {
			for (int i = 0; i < checkedKeys.size(); i++) {
				if(checkedKeys.getString(i).indexOf(IrrigationConstants.机井树节点类型_机井.getCode())>-1) {
					empPumpWell = new IrrigationEmpPumpWell();
					addIrrigationEmpPumpWell(empPumpWellList,projectNo,empNo, checkedKeys.getString(i), empPumpWell);
				}else {
					empField = new IrrigationEmpField();
					addIrrigationEmpField(empFieldList,projectNo,empNo, checkedKeys.getString(i), empField);
				}
			}
		}
		
		if(empFieldList.size()>0) {
			iIrrigationEmpFieldService.saveBatch(empFieldList);
		}
		if(empPumpWellList.size()>0) {
			saveBatch(empPumpWellList);
		}
	}

	@Override
	public void removeEmpPumpWellByEmpNo(String projectNo,String empNo) {
		LambdaQueryWrapper<IrrigationEmpPumpWell> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(IrrigationEmpPumpWell::getEmpNo, empNo);
		lambdaQueryWrapper.eq(IrrigationEmpPumpWell::getProjectNo, projectNo);
		remove(lambdaQueryWrapper);
	}
	
	
	
	private void addIrrigationEmpPumpWell(List<IrrigationEmpPumpWell> empPumpWellList,String projectNo,String empNo,String nodeKey,IrrigationEmpPumpWell empPumpWell) {
		String pwNo = nodeKey.substring(nodeKey.indexOf("#")+1);
		empPumpWell.setProjectNo(projectNo);
		empPumpWell.setEmpNo(empNo);
		empPumpWell.setPwNo(pwNo);
		empPumpWell.setCreateTime(LocalDateTime.now());
		empPumpWell.setUpdateTime(LocalDateTime.now());
		empPumpWell.setNodeKey(nodeKey);
		empPumpWellList.add(empPumpWell);
	}
	
	private void addIrrigationEmpField(List<IrrigationEmpField> empFieldList,String projectNo,String empNo,String nodeKey,IrrigationEmpField empField) {
		String fieldNo = nodeKey.substring(nodeKey.indexOf("#")+1);
		empField.setProjectNo(projectNo);
		empField.setEmpNo(empNo);
		empField.setFieldNo(fieldNo);
		empField.setCreateTime(LocalDateTime.now());
		empField.setUpdateTime(LocalDateTime.now());
		empField.setNodeKey(nodeKey);
		empFieldList.add(empField);
	}

	@Override
	public List<String> queryEmpPumpWellNodeKeys(String projectNo, String empNo) {
		LambdaQueryWrapper<IrrigationEmpPumpWell> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(IrrigationEmpPumpWell::getEmpNo, empNo);
		lambdaQueryWrapper.eq(IrrigationEmpPumpWell::getProjectNo, projectNo);
		List<IrrigationEmpPumpWell> empPumpWellList = list(lambdaQueryWrapper);
		List<String> nodeKeys = new ArrayList<>();
		for(IrrigationEmpPumpWell pump : empPumpWellList) {
			nodeKeys.add(pump.getNodeKey());
		}
		return nodeKeys;
	}

	@Override
	public <T> List<T> viewList(QueryWrapper<T> queryWrapper, DataCenter dc) {
		return irrigationEmpPumpWellMapper.viewList(queryWrapper, dc);
	}

	@Override
	public <T> int viewCount(QueryWrapper<T> queryWrapper) {
		return irrigationEmpPumpWellMapper.viewCount(queryWrapper);
	}

	@Override
	public <T> IPage<T> viewPage(IPage<T> page, QueryWrapper<T> queryWrapper, DataCenter dc) {
		return irrigationEmpPumpWellMapper.viewPage(page, queryWrapper, dc);
	}
}
